/*
 * Copyright (C) 2017 zhouyou(478319399@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shuyihui.rxhttp.body

import android.os.Handler
import android.os.Looper
import android.os.Message
import java.io.Serializable
import java.lang.ref.WeakReference

/**
 *
 * 描述：可以直接更新UI的回调
 */
abstract class UIProgressResponseCallBack : ProgressResponseCallBack {
    //处理UI层的Handler子类
    private class UIHandler(
        looper: Looper?,
        uiProgressResponseListener: UIProgressResponseCallBack
    ) : Handler(
        looper!!
    ) {
        //弱引用
        private val mUIProgressResponseListenerWeakReference: WeakReference<UIProgressResponseCallBack>
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                RESPONSE_UPDATE -> {
                    val uiProgressResponseListener = mUIProgressResponseListenerWeakReference.get()
                    if (uiProgressResponseListener != null) {
                        //获得进度实体类
                        val progressModel = msg.obj as ProgressModel
                        //回调抽象方法
                        uiProgressResponseListener.onUIResponseProgress(
                            progressModel.currentBytes,
                            progressModel.contentLength,
                            progressModel.isDone
                        )
                    }
                }
                else -> super.handleMessage(msg)
            }
        }

        init {
            mUIProgressResponseListenerWeakReference = WeakReference(uiProgressResponseListener)
        }
    }

    //主线程Handler
    private val mHandler: Handler = UIHandler(Looper.getMainLooper(), this)
    override fun onResponseProgress(bytesWritten: Long, contentLength: Long, done: Boolean) {
        //通过Handler发送进度消息
        val message = Message.obtain()
        message.obj = ProgressModel(bytesWritten, contentLength, done)
        message.what = RESPONSE_UPDATE
        mHandler.sendMessage(message)
    }

    /**
     * UI层回调抽象方法
     *
     * @param bytesRead     当前读取响应体字节长度
     * @param contentLength 总字节长度
     * @param done          是否读取完成
     */
    abstract fun onUIResponseProgress(bytesRead: Long, contentLength: Long, done: Boolean)
    inner class ProgressModel(//当前读取字节长度
        var currentBytes: Long, //总字节长度
        var contentLength: Long, //是否读取完成
        var isDone: Boolean
    ) : Serializable {
        fun setCurrentBytes(currentBytes: Long): ProgressModel {
            this.currentBytes = currentBytes
            return this
        }

        fun setContentLength(contentLength: Long): ProgressModel {
            this.contentLength = contentLength
            return this
        }

        fun setDone(done: Boolean): ProgressModel {
            isDone = done
            return this
        }

        override fun toString(): String {
            return "ProgressModel{" +
                    "currentBytes=" + currentBytes +
                    ", contentLength=" + contentLength +
                    ", done=" + isDone +
                    '}'
        }
    }

    companion object {
        private const val RESPONSE_UPDATE = 0x02
    }
}